package com.weishi.project.training.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.project.sport.trainingdata.vo.TypeValueVO;
import com.weishi.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.weishi.project.training.mapper.TrainingLocationMapper;
import com.weishi.project.training.domain.TrainingLocation;
import com.weishi.project.training.service.ITrainingLocationService;

/**
 * 【请填写功能名称】Service业务层处理
 */
@Service
public class TrainingLocationServiceImpl extends ServiceImpl<TrainingLocationMapper, TrainingLocation> implements ITrainingLocationService {

    @Autowired
    private TrainingLocationMapper trainingLocationMapper;

    /**
     * 数据字典接口
     */
    @Autowired
    private ISysDictDataService dictDataService;



    private static final String AK = "eyIdZ2PVrv0CSednjsgfx5LX7zqv25kh";


    @Override
    public List<TrainingLocation> selectTrainingLocationList(TrainingLocation trainingLocation) {
        return trainingLocationMapper.selectTrainingLocationList(trainingLocation);
    }


    /**
     * Baidu地图通过地址获取经纬度
     */
    @Override
    public Map<String, Double> getLngAndLat(TrainingLocation trainingLocation) {
        Map<String, Double> map = new HashMap<String, Double>();
        String address = trainingLocation.getProvince()+trainingLocation.getCity()+trainingLocation.getCounty();
        String url = "http://api.map.baidu.com/geocoding/v3/?address=" + address
                + "&output=json&ak="+AK+"&callback=showLocation";
        try {
            String json = loadJSON(url);

            json = json.replace("showLocation&&showLocation(", "");
            json = json.substring(0, json.length() - 1);
            JSONObject obj = JSONObject.parseObject(json);
            if (obj.get("status").toString().equals("0")) {
                double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
                double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
                map.put("lng", lng);
                map.put("lat", lat);
            }
        } catch (Exception e) {
            System.out.println("未找到相匹配的经纬度，请检查地址！");
        }
        return map;
    }

    @Override
    public int getTrainingLocationCount(Map<String, Object> map) {
        return trainingLocationMapper.getTrainingLocationCount(map);
    }

    @Override
    public List<TrainingLocation> selectTrainingLocationListByMap(Map<String, Object> params) {
        return trainingLocationMapper.selectTrainingLocationListByMap(params);
    }


    public  static String loadJSON(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }



    /**
     * 查询训练地址详情
     */
    @Override
    public TrainingLocation getInfo(Integer id) {
        TrainingLocation trainingLocation = this.getById(id);
        // 查询数据字典训练地点
        List<TypeValueVO> typeValueVOList = dictDataService.getTypeValue("adress_type");
        if(typeValueVOList.size() > 0)typeValueVOList.forEach( p-> {
            if(p.getDictValue().equals(trainingLocation.getType().toString())){
                trainingLocation.setName(p.getDictLabel());
            }
        });
        return trainingLocation;
    }









}